(function() {
  if (window.google && google.friendconnect) {
    // retrieve gfc information
    var siteId = google.friendconnect.container.openSocialSiteId;
    var parentUrl = google.friendconnect.container.J || '/'; // "J" seems parentUrl (obfuscated)

    // create overlaid element and iframe
    var div = document.createElement('div');
    div.style.backgroundColor = '#fff';
    div.style.position = 'fixed';
    div.style.top = div.style.left = '0px';
    div.style.width = div.style.height = '100%';
    div.style.zIndex = 99999; 
    div.style.padding = '5px';
    div.innerHTML = '<a href="#" style="color:#a00" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">close</a><br/>';
    var ifr = document.createElement('iframe');
    var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) - 30;
    var h = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - 40;
    ifr.style.width = w+'px';
    ifr.style.height = h+'px';
    document.body.appendChild(div);
    div.appendChild(ifr);

    // read query parameters in script src url hash part
    var params = {};
    var scripts = Array.prototype.slice.apply(document.getElementsByTagName('script'));
    for (var script = scripts.pop(); script; script = scripts.pop()) { if (script.src) break }
    var query = script.src.split('#')[1];
    if (!query) { alert('no script parameter passed'); return }
    var pairs = query.split('&');
    for (var i=0; i<pairs.length; i++) {
      var pair = pairs[i].split('=');
      params[pair[0]] = pair[1];
    }

    // writing iframe content
    var d = ifr.contentWindow.document;
    d.open();
    var html = [
      '<html><head>',
      '<script type="text/javascript" src="http://www.google.com/jsapi"></script>',
      '<script type="text/javascript">',
      "google.load('friendconnect', '0.8');",
      '</script>',
      '<script type="text/javascript">',
      "function init() {",
      params.type == 'gadget' ? [
      "  google.friendconnect.container.renderOpenSocialGadget({",
      "    id : 'body',",
      "    url : '"+params.url+"',",
      params.presentation ?  
      "    presentation : '"+params.presentation+"'," : "",
      "    site : '"+siteId+"'",
      "});",
      ].join('\n') : [
      "  var s = document.createElement('script');",
      "  s.type = 'text/javascript';",
      "  s.src = '"+params.url+"';",
      "  document.getElementsByTagName('head')[0].appendChild(s);",
      ].join('\n'),
      "}",
      "google.friendconnect.container.setParentUrl('"+parentUrl+"');",
      "google.friendconnect.container.initOpenSocialApi({",
      "  site : '"+siteId+"', onload : init });",
      "setTimeout(function(){document.close();},100);", // delayed close
      '</script>',
      '</head>',
      '<body id="body"></body>',
      '</html>'
    ].join('\n');
    d.write(html);
    //d.close(); // don't close doc, friendconnect does document.write in script.
  } else {
    alert('no friendconnect info in this site.');
  }
})();
